package com.oron3.lock;

import java.util.ArrayList;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.text.InputType;

import com.ofek.applicationbase.forms.FieldQConnector;
import com.ofek.applicationbase.forms.FormActivity;
import com.ofek.applicationbase.forms.QuestionViewController;
import com.ofek.applicationbase.forms.SimpleQuestionView;
import com.oron3.lock.LockDatabaseService.LockServiceBinder;
import com.oron3.locker.R;

public class SecretFormActivity extends FormActivity {
	public final static String KEY_INTENT_EXTRA_SECRET_TO_EDIT = "com.oron3.Lock.KEY_INTENT_EXTRA_SECRET_TO_EDIT";
	private Secret secret;
	private AES aes;
	@Override
	protected void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_secret_form);
		if ((secret = (Secret) getIntent().getSerializableExtra(KEY_INTENT_EXTRA_SECRET_TO_EDIT))!=null) 
			setTitle(R.string.edit);
		else {
			setTitle(R.string.add);
			secret = new Secret();
		}
		try {
			aes = new AES(getIntent().getStringExtra(LockActivity.KEY_INTENT_EXTRA_UC));
		} catch (final Exception e) {
			e.printStackTrace();
		} 
		initForm();
	}
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	public void onFormSubmitted() {
		setProgressBarVisibility(true);
		bindService(new Intent(this,LockDatabaseService.class),new ServiceConnection() {

			@Override
			public void onServiceDisconnected(final ComponentName name) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onServiceConnected(final ComponentName name, final IBinder binder) {
				final LockServiceBinder binder1 = (LockServiceBinder)binder;
				final LockDatabaseService service = binder1.getLockDatabaseService();
				try {
					secret.text = aes.encrypt(secret.text);
					secret.title = aes.encrypt(secret.title);
				} catch (final Exception e) {
					e.printStackTrace();
				}
				if (getIntent().hasExtra(KEY_INTENT_EXTRA_SECRET_TO_EDIT)) service.update(secret);
				else service.add(secret);

				unbindService(this);
				//				final Intent intent = new Intent(SecretFormActivity.this,LockActivity.class);
				//				intent.putExtra(LockActivity.KEY_INTENT_EXTRA_UC, getIntent().getStringExtra(LockActivity.KEY_INTENT_EXTRA_UC));
				//				startActivity(intent);
				finish();
			}
		},BIND_AUTO_CREATE);


	}

	@Override
	protected ArrayList<QuestionViewController> createQuestions() {
		final ArrayList<QuestionViewController> arrayList = new ArrayList<QuestionViewController>();
		try {
			arrayList.add(new FieldQConnector("title", secret, new SimpleQuestionView(R.string.title,InputType.TYPE_CLASS_TEXT,this,true)) );
			arrayList.add(new FieldQConnector("text", secret, new SimpleQuestionView(R.string.secret,InputType.TYPE_CLASS_TEXT,this,true)) );
		} catch (final Exception e) {
			e.printStackTrace();
		}
		return arrayList;
	}

}
